//
//  EFAppContext.m
//  EFHealth
//
//  Created by liumadu on 14-11-28.
//  Copyright (c) 2018年 ef. All rights reserved.
//

#import "EFAppContext.h"
#import "UICKeyChainStore.h"


static NSString *const EFAppContextUserName = @"userName";
static NSString *const EFAppContextPassWard = @"passWard";

static NSString *const EFAppContextNF_ACCESS_TOKEN = @"ACCESS_TOKEN";

static NSString *const EFAppContextUserid = @"userid";

static NSString *const EFAppContextBirthday = @"birthday";
static NSString *const EFAppContextHeight = @"height";
static NSString *const EFAppContextweight = @"weight";
static NSString *const EFAppContextHeadUrl = @"headUrl";
static NSString *const EFAppContextIdcard = @"idcard";
static NSString *const EFAppContextNational = @"national";
static NSString *const EFAppContextNick = @"nick";
static NSString *const EFAppContextRname = @"rname";
static NSString *const EFAppContextSex = @"sex";
static NSString *const EFAppContextSick_history = @"sick_history";

static NSString *const EFMemberId = @"memberId";
static NSString *const EFAuthorization = @"authorization";
static NSString *const EFMobile = @"mobile";
static NSString *const EFUnitId = @"unitId";


@implementation EFAppContext

- (id)init
{ 
    if (self = [super init]) {
       
        self.userName = [UICKeyChainStore stringForKey:EFAppContextUserName] ;
        self.passWard = [UICKeyChainStore stringForKey:EFAppContextPassWard] ;
        
        self.ACCESS_TOKEN = EFUserDefaultsObjForKey(EFAppContextNF_ACCESS_TOKEN);
        
        self.userid = [UICKeyChainStore stringForKey:EFAppContextUserid];
        self.birthday = [UICKeyChainStore stringForKey:EFAppContextBirthday];
        self.height = [UICKeyChainStore stringForKey:EFAppContextHeight];
        self.weight = [UICKeyChainStore stringForKey:EFAppContextweight];
        self.headUrl = [UICKeyChainStore stringForKey:EFAppContextHeadUrl];
        self.idcard = [UICKeyChainStore stringForKey:EFAppContextIdcard];
        self.national = [UICKeyChainStore stringForKey:EFAppContextNational];
        
        self.nick = [UICKeyChainStore stringForKey:EFAppContextNick];
        self.rname = [UICKeyChainStore stringForKey:EFAppContextRname];
        self.sex = [UICKeyChainStore stringForKey:EFAppContextSex];
        self.sick_history = [UICKeyChainStore stringForKey:EFAppContextSick_history];
        
        self.memberId = [UICKeyChainStore stringForKey:EFMemberId];
        self.authorization = [UICKeyChainStore stringForKey:EFAuthorization];
        self.mobile = [UICKeyChainStore stringForKey:EFMobile];
        self.unitId = [UICKeyChainStore stringForKey:EFUnitId];
    }
    return self;
}

+ (instancetype)sharedContext
{
    static EFAppContext *_sharedContext = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedContext = [[EFAppContext alloc] init];
    });
    return _sharedContext;
}

- (void)save
{
    if (!ISEMPTY(self.userName)) {
        [UICKeyChainStore setString:self.userName forKey:EFAppContextUserName];
    }
    if (!ISEMPTY(self.passWard)) {
        [UICKeyChainStore setString:self.passWard forKey:EFAppContextPassWard];
    }
    
    if (!ISEMPTY(self.ACCESS_TOKEN)) {
        EFUserDefaultsSetObj(self.ACCESS_TOKEN, EFAppContextNF_ACCESS_TOKEN);
    }
    if (!ISEMPTY(self.userid)) {
        [UICKeyChainStore setString:self.userid forKey:EFAppContextUserid];
    }
    if (!ISEMPTY(self.birthday)) {
        [UICKeyChainStore setString:self.birthday forKey:EFAppContextBirthday];
    }
    if (!ISEMPTY(self.height)) {
        [UICKeyChainStore setString:self.height forKey:EFAppContextHeight];
    }
    if (!ISEMPTY(self.weight)) {
        [UICKeyChainStore setString:self.weight forKey:EFAppContextweight];
    }
    if (!ISEMPTY(self.idcard)) {
        [UICKeyChainStore setString:self.idcard forKey:EFAppContextIdcard];
    }
    if (!ISEMPTY(self.national)) {
        [UICKeyChainStore setString:self.national forKey:EFAppContextNational];
    }
    if (!ISEMPTY(self.nick)) {
        [UICKeyChainStore setString:self.nick forKey:EFAppContextNick];
    }
    if (!ISEMPTY(self.rname)) {
        [UICKeyChainStore setString:self.rname forKey:EFAppContextRname];
    }
    if (!ISEMPTY(self.headUrl)) {
        [UICKeyChainStore setString:self.headUrl forKey:EFAppContextHeadUrl];
    }
    if (!ISEMPTY(self.sex)) {
        [UICKeyChainStore setString:self.sex forKey:EFAppContextSex];
    }
    if (!ISEMPTY(self.sick_history)) {
        [UICKeyChainStore setString:self.sick_history forKey:EFAppContextSick_history];
    }
    if (!ISEMPTY(self.memberId)) {
        [UICKeyChainStore setString:self.memberId forKey:EFMemberId];
    }
    if (!ISEMPTY(self.authorization)) {
        [UICKeyChainStore setString:self.authorization forKey:EFAuthorization];
    }
    if (!ISEMPTY(self.mobile)) {
        [UICKeyChainStore setString:self.mobile forKey:EFMobile];
    }
    if (!ISEMPTY(self.unitId)) {
        [UICKeyChainStore setString:self.unitId forKey:EFUnitId];
    }
}
- (void)clear
{
//    [UICKeyChainStore removeItemForKey:EFAppContextUserName];
//    self.userName = nil ;
//    
//    [UICKeyChainStore removeItemForKey:EFAppContextPassWard];
//    self.passWard = nil ;
    
    // 删除用户UID记录
    EFUserDefaultsSetObj(@"", EFAppContextNF_ACCESS_TOKEN)
    self.ACCESS_TOKEN = nil;
   
    [UICKeyChainStore removeItemForKey:EFAppContextUserid];
    self.userid = nil ;
    [UICKeyChainStore removeItemForKey:EFAppContextBirthday];
    self.birthday = nil ;
    [UICKeyChainStore removeItemForKey:EFAppContextHeight];
    self.height = nil ;
    [UICKeyChainStore removeItemForKey:EFAppContextweight];
    self.weight = nil ;
    [UICKeyChainStore removeItemForKey:EFAppContextHeadUrl];
    self.headUrl = nil ;
    [UICKeyChainStore removeItemForKey:EFAppContextIdcard];
    self.idcard = nil ;
    [UICKeyChainStore removeItemForKey:EFAppContextNational];
    self.national = nil ;
    [UICKeyChainStore removeItemForKey:EFAppContextNick];
    self.nick = nil ;
    [UICKeyChainStore removeItemForKey:EFAppContextRname];
    self.rname = nil ;
    [UICKeyChainStore removeItemForKey:EFAppContextSex];
    self.sex = nil ;
    [UICKeyChainStore removeItemForKey:EFMemberId];
    self.memberId = nil ;
    [UICKeyChainStore removeItemForKey:EFAuthorization];
    self.authorization = nil ;
    [UICKeyChainStore removeItemForKey:EFMobile];
    self.mobile = nil ;
    [UICKeyChainStore removeItemForKey:EFAppContextSick_history];
    self.sick_history = nil ;
    [UICKeyChainStore removeItemForKey:EFUnitId];
    self.unitId = nil ;
    
}

- (NSString *)userRealName
{
    if (!ISEMPTY(self.rname)) {
        return self.rname ;
    }
    if (!ISEMPTY(self.nick)) {
        return self.nick ;
    }
    return @"";
}
+ (BOOL)isUserLogin
{
    EFAppContext *appc = [EFAppContext sharedContext];
    if (ISEMPTY(appc.ACCESS_TOKEN)) {
        return NO ;
    }
    else{
        return YES ;
    }
//    return !ISEMPTY(appc.ACCESS_TOKEN);
}
@end
